<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Feed{
	var $CI;
	function __destruct() {}
	function __construct() {
		$this->CI = &get_instance();
		error_reporting(0);
	}
	
	function clearTeamsCache(){
		$mode = array('Livescore', 'Fixtures');
		foreach($mode as $type){
			$this->CI->pyrocache->delete('team_'.$type);
			$this->getArrayTeams($type);	
		}
		return true;
	}
	
	function getFeedInfoWithClosedMatch($type,$key,$match_id){
		$linkArray = defineFeedArrayData();
		if(!$type OR !$key OR !$match_id) return false;
		
		if(isset($linkArray[$type][$key])){
			$feedArray =  $linkArray[$type][$key];
			$feedLink = $feedArray['link'];
			$string = file_get_contents($feedLink);
			$xml = new SimpleXMLElement($string);
			$res = $xml->xpath("//category/match[@id='$match_id']"); 
			
			$comment = $this->getCommentaries($type,$key,$match_id);
			
			if($res AND $comment){
				return array(
								'type'	=>$type,
								'key'	=>$key,
								'match_id' => $match_id	
							);
			}
		} 
		
		$array_info = array(
			'type'	=>$type,
			'key'	=>$key,
			'match_id' => $match_id	
		);
		foreach($linkArray as $type => $feedArray){
			$link_type = $type;
			foreach($feedArray as $key => $valueArray){
				$link_key = $key;
				$xmlLink = $valueArray['link'];
				$string = file_get_contents($xmlLink);
				try{
					$xml = new SimpleXMLElement($string);
					$res = $xml->xpath("//category/match[@id='$match_id']"); 
					for($i = 0; $i< sizeof($res); $i++){
						$xml_match_id = (string) $res[$i]->attributes()->id;
						$comment = $this->getCommentaries($link_type,$link_key,$xml_match_id);
						if($xml_match_id == $match_id AND $comment){
							$array_info = array(
								'type'	=>$link_type,
								'key'	=>$link_key,
								'match_id' => $xml_match_id	
							);
							return $array_info;
							break;
						}
					}
				}catch(Exception $e){
					//echo 'Message: ' .$e->getMessage();
				}	
			}
		}
		return $array_info;
	
	}
	
	function getFeedInfoWithMatchId($type,$key,$match_id){
		if(!$type OR !$key OR !$match_id) return false;
		
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//category/match[@id='$match_id']"); 
		
		$comment = $this->getCommentaries($type,$key,$match_id);
		$info = $this->getCurrentMatchInfo($type,$key,$match_id);
		if(!$info){
			return false;
		}
		
		if($info['comment'] == 'Not started'){
			return array(
							'type'	=>$type,
							'key'	=>$key,
							'match_id' => $match_id	
						);
		}
		
		if($res AND $comment){
			return array(
							'type'	=>$type,
							'key'	=>$key,
							'match_id' => $match_id	
						);
		}
		 
		$array_info = array(
			'type'	=>$type,
			'key'	=>$key,
			'match_id' => $match_id	
		);
		foreach($linkArray as $type => $feedArray){
			$link_type = $type;
			foreach($feedArray as $key => $valueArray){
				$link_key = $key;
				$xmlLink = $valueArray['link'];
				$string = file_get_contents($xmlLink);
				try{
					$xml = new SimpleXMLElement($string);
					$res = $xml->xpath("//category/match[@id='$match_id']"); 
					for($i = 0; $i< sizeof($res); $i++){
						$xml_match_id = (string) $res[$i]->attributes()->id;
						$comment = $this->getCommentaries($link_type,$link_key,$xml_match_id);
						if($xml_match_id == $match_id AND $comment){
							$array_info = array(
								'type'	=>$link_type,
								'key'	=>$link_key,
								'match_id' => $xml_match_id	
							);
							return $array_info;
							break;
						}
					}
				}catch(Exception $e){
					//echo 'Message: ' .$e->getMessage();
				}		
			}
		}
		return $array_info;
	}
	
	function getArrayTeams($type = 'Livescore'){
		$linkArray = defineFeedArrayData();
		$linksArr = $linkArray[$type];
		$arrayTeam = array();
		if ( ! $arrayTeam = $this->CI->pyrocache->get('team_'.$type)){
			foreach($linksArr as $key=> $val){
				$string = file_get_contents($val['link']);
				$xml = new SimpleXMLElement($string);
				$res = $xml->xpath("//category/match"); 
				for($i = 0; $i< sizeof($res); $i++){
					$localTeam = $res[$i]->localteam->attributes();
					$visitorTeam = $res[$i]->visitorteam->attributes();
					$localName = $localTeam->name;
					$localId = $localTeam['id'];
					$visitorName = $visitorTeam->name;
					$visitorId = $visitorTeam['id'];
					$arrayTeam["$localId"] = (string) $localName;
					$arrayTeam["$visitorId"] = (string) $visitorName;
				}
			}
			array_filter($arrayTeam);
			$this->CI->pyrocache->write($arrayTeam, 'team_'.$type);
		}
		return $arrayTeam;
	}
	
	function getBaseInfo($type, $key){
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//category"); 
		
		if(!$res){return false;}
		
		$arrayReturn = array();
		for($i = 0; $i< sizeof($res); $i++){
			$arrayReturn[] = array( 'category_name' => isset($res[$i]->attributes()->name) ? (string) $res[$i]->attributes()->name : '',
									'category_id' => isset($res[$i]->attributes()->id) ? (string) $res[$i]->attributes()->id : '',
									'match_id' => isset($res[$i]->match->attributes()->id) ?(string) $res[$i]->match->attributes()->id : '',
									'match_date' => isset($res[$i]->match->attributes()->date) ?(string) $res[$i]->match->attributes()->date : '',
									'match_time' => isset($res[$i]->match->attributes()->time) ?(string) $res[$i]->match->attributes()->time :'',
									'match_status' => isset($res[$i]->match->attributes()->status) ?(string) $res[$i]->match->attributes()->status:'',
									'match_formatted_date' => isset($res[$i]->match->attributes()->formatted_date) ?(string) $res[$i]->match->attributes()->formatted_date :'',
									'localteam_name' => isset($res[$i]->match->localteam->attributes()->name) ?(string) $res[$i]->match->localteam->attributes()->name:'',
									'localteam_totalscore' => isset($res[$i]->match->localteam->attributes()->totalscore) ?(string) $res[$i]->match->localteam->attributes()->totalscore:'',
									'localteam_type' => isset($res[$i]->match->localteam->attributes()->type) ?(string) $res[$i]->match->localteam->attributes()->type :'',
									'localteam_stat' => isset($res[$i]->match->localteam->attributes()->stat) ?(string) $res[$i]->match->localteam->attributes()->stat :'',
									'localteam_id' => isset($res[$i]->match->localteam->attributes()->id) ?(string) $res[$i]->match->localteam->attributes()->id :'',
									'visitorteam_name' => isset($res[$i]->match->visitorteam->attributes()->name) ? (string) $res[$i]->match->visitorteam->attributes()->name :'',
									'visitorteam_totalscore' => isset($res[$i]->match->visitorteam->attributes()->totalscore) ?(string) $res[$i]->match->visitorteam->attributes()->totalscore :'',
									'visitorteam_type' => isset($res[$i]->match->visitorteam->attributes()->type) ?(string) $res[$i]->match->visitorteam->attributes()->type :'',
									'visitorteam_stat' => isset($res[$i]->match->visitorteam->attributes()->stat)?(string) $res[$i]->match->visitorteam->attributes()->stat:'',
									'visitorteam_id' => isset($res[$i]->match->visitorteam->attributes()->id) ?(string) $res[$i]->match->visitorteam->attributes()->id:'',
									'comment' => isset($res[$i]->match->comment->attributes()->post) ?(string) $res[$i]->match->comment->attributes()->post :'',
							);
			
		}
		return $arrayReturn;
	}
	
	function getCategoryAttr($type,$key,$category_id,$match_id){
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//category[@id='$category_id']"); 
		
		if(!$res){ return false;}
		$array = array();
		for($i=0;$i<sizeof($res);$i++){
			$match_id_feed = (string) $res[$i]->match->attributes()->id;
			if( $match_id_feed == $match_id ){
				$array = array(
							'name'	=> (string) $res[$i]->attributes()->name,
							'id'	=> (string) $res[$i]->attributes()->id,
						);
				break;		
			}
		}
		return $array;
	}
	
	function getCurrentMatchInfo($type,$key,$match_id){
		//$info = $this->getFeedInfoWithMatchId($type,$key,$match_id);
		//if(empty($info)){ return false;}
		
		//$type = $info['type'];
		//$key = $info['key'];
		if(!$type OR !$key OR !$match_id) return false;
		
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//match[@id='$match_id']"); 
		
		if(!$res){return false;}
		
		$arrayReturn = array(
							'match_id' => isset($res[0]->attributes()->id) ?(string) $res[0]->attributes()->id :'',
							'match_date' => isset($res[0]->attributes()->date) ?(string) $res[0]->attributes()->date :'',
							'match_time' => isset($res[0]->attributes()->time) ?(string) $res[0]->attributes()->time :'',
							'match_status' => isset($res[0]->attributes()->status) ?(string) $res[0]->attributes()->status :'',
							'match_formatted_date' => isset($res[0]->attributes()->formatted_date) ?(string) $res[0]->attributes()->formatted_date :'',
							'localteam_name' => isset($res[0]->localteam->attributes()->name) ?(string) $res[0]->localteam->attributes()->name :'',
							'localteam_totalscore' => isset($res[0]->localteam->attributes()->totalscore) ?(string) $res[0]->localteam->attributes()->totalscore :'',
							'localteam_type' => isset($res[0]->localteam->attributes()->type) ?(string) $res[0]->localteam->attributes()->type :'',
							'localteam_stat' => isset($res[0]->localteam->attributes()->stat) ?(string) $res[0]->localteam->attributes()->stat :'',
							'localteam_id' => isset($res[0]->localteam->attributes()->id) ?(string) $res[0]->localteam->attributes()->id :'',
							'visitorteam_name' => isset($res[0]->visitorteam->attributes()->name) ?(string) $res[0]->visitorteam->attributes()->name :'',
							'visitorteam_totalscore' => isset($res[0]->visitorteam->attributes()->totalscore) ?(string) $res[0]->visitorteam->attributes()->totalscore :'',
							'visitorteam_type' => isset($res[0]->visitorteam->attributes()->type) ?(string) $res[0]->visitorteam->attributes()->type :'',
							'visitorteam_stat' => isset($res[0]->visitorteam->attributes()->stat) ?(string) $res[0]->visitorteam->attributes()->stat :'',
							'visitorteam_id' => isset($res[0]->visitorteam->attributes()->id) ?(string) $res[0]->visitorteam->attributes()->id :'',
							'comment' => isset($res[0]->comment->attributes()->post) ?(string) $res[0]->comment->attributes()->post :'',
						);
		return $arrayReturn;					
	}

	function getInning($type,$key,$match_id){
		//$info = $this->getFeedInfoWithMatchId($type,$key,$match_id);
		//if(empty($info)){ return false;}
		
		//$type = $info['type'];
		//$key = $info['key'];
		if(!$type OR !$key OR !$match_id) return false;
		
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//match[@id='$match_id']"); 
		
		if(!$res){return false;}
		
		$arrayReturn = array();

		$i = 0;
		while($res[0]->inning[$i]){
			$attr = array(
				'name' => isset($res[0]->inning[$i]->attributes()->name) ?(string) $res[0]->inning[$i]->attributes()->name :'',
				'team' => isset($res[0]->inning[$i]->attributes()->team) ?(string) $res[0]->inning[$i]->attributes()->team :'',
				'inningnum' => isset($res[0]->inning[$i]->attributes()->inningnum) ?(string) $res[0]->inning[$i]->attributes()->inningnum :''
			);
			$batsmanstats  = array();
			$j = 0;
			while($res[0]->inning[$i]->batsmanstats->player[$j]){
				$batsmanstats[] = array(
					'batsman'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->batsman) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->batsman :'',
					'status'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->status) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->status :'',
					'r'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->r) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->r :'',
					'b'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->b) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->b :'',
					's4'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->s4) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->s4 :'',
					's6'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->s6) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->s6 :'',
					'sr'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->sr) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->sr :'',
					'bat'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->bat) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->bat :'',
					'id'	=> isset($res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->id) ?(string) $res[0]->inning[$i]->batsmanstats->player[$j]->attributes()->id :''
				);
				$j++;
			}
			$totals = array(
				'ext' => isset($res[0]->inning[$i]->totals->attributes()->ext) ?(string) $res[0]->inning[$i]->totals->attributes()->ext :'',
				'b' => isset($res[0]->inning[$i]->totals->attributes()->b) ?(string) $res[0]->inning[$i]->totals->attributes()->b :'',
				'lb' => isset($res[0]->inning[$i]->totals->attributes()->lb) ?(string) $res[0]->inning[$i]->totals->attributes()->lb :'',
				'nb' => isset($res[0]->inning[$i]->totals->attributes()->nb) ?(string) $res[0]->inning[$i]->totals->attributes()->nb :'',
				'wd' => isset($res[0]->inning[$i]->totals->attributes()->wd) ?(string) $res[0]->inning[$i]->totals->attributes()->wd :'',
				'p' => isset( $res[0]->inning[$i]->totals->attributes()->p) ?(string) $res[0]->inning[$i]->totals->attributes()->p :'',
				'tot' => isset($res[0]->inning[$i]->totals->attributes()->tot) ?(string) $res[0]->inning[$i]->totals->attributes()->tot :'',
				'rr' => isset($res[0]->inning[$i]->totals->attributes()->rr) ?(string) $res[0]->inning[$i]->totals->attributes()->rr :'',
			);
			$bowlers  = array();
			$j = 0;
			while($res[0]->inning[$i]->bowlers->player[$j]){
				$bowlers[] = array(
					'bowler'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->bowler) ? (string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->bowler :'',
					'o'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->o) ? (string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->o : '',
					'm'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->m) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->m :'',
					'r'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->r) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->r :'',
					'w'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->w) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->w :'',
					'nb'	=> isset( $res[0]->inning[$i]->bowlers->player[$j]->attributes()->nb) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->nb :'',
					'wd'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->wd) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->wd :'',
					'er'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->er) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->er :'',
					'ball'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->ball) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->ball :'',
					'id'	=> isset($res[0]->inning[$i]->bowlers->player[$j]->attributes()->id) ?(string) $res[0]->inning[$i]->bowlers->player[$j]->attributes()->id :'',
				);
				$j++;
			}
			$partnerships = array();
			$j = 0;
			while($res[0]->inning[$i]->partnerships->partnership[$j]){
				$partnerships[] = array(
					'partnership'	=> isset($res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->partnership) ? (string) $res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->partnership :'',
					'rb1'	=> isset($res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->rb1) ?(string) $res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->rb1 :'',
					'rb2'	=> isset($res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->rb2) ?(string) $res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->rb2 :'',
					'ex'	=> isset($res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->ex) ?(string) $res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->ex :'',
					'total'	=> isset($res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->total) ?(string) $res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->total :'',
					'id'	=> isset($res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->id) ?(string) $res[0]->inning[$i]->partnerships->partnership[$j]->attributes()->id :'',
				);
				$j++;
			}
			$arrayReturn[] = array(
				'attributes'	=> $attr,
				'batsmanstats'	=> $batsmanstats,
				'totals'		=> $totals,
				'bowlers'		=> $bowlers,
				'partnerships'	=> $partnerships
			);
			$i++;
		}
		return $arrayReturn;
	}
	
	function getCommentaries($type,$key,$match_id){
		//$info = $this->getFeedInfoWithMatchId($type,$key,$match_id);
		//if(empty($info)){ return false;}
		
		//$type = $info['type'];
		//$key = $info['key'];
		if(!$type OR !$key OR !$match_id) return false;
		
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//match[@id='$match_id']"); 
		
		if(!$res){return false;}
		
		$commentaries = array();
		$j = 0;
		while($res[0]->commentaries->commentary[$j]){
			if($key == 'livescore'){
				$commentaries[] = array(
					'post'	=> isset($res[0]->commentaries->commentary[$j]->attributes()->post) ?(string) $res[0]->commentaries->commentary[$j]->attributes()->post :'',
					'over'	=> isset($res[0]->commentaries->commentary[$j]->attributes()->over) ?(string) $res[0]->commentaries->commentary[$j]->attributes()->over :'',
					'name'	=> ''//isset($res[0]->commentaries->commentary[$j]->attributes()->name) ?(string) $res[0]->commentaries->commentary[$j]->attributes()->name :'',
				);
			}else{
				$commentaries[] = array(
					'post'	=> isset($res[0]->commentaries->commentary[$j]->attributes()->post) ?(string) $res[0]->commentaries->commentary[$j]->attributes()->post :'',
					'point'	=> isset($res[0]->commentaries->commentary[$j]->attributes()->point) ?(string) $res[0]->commentaries->commentary[$j]->attributes()->point :'',
					'name'	=> isset($res[0]->commentaries->commentary[$j]->attributes()->name) ?(string) $res[0]->commentaries->commentary[$j]->attributes()->name :'',
				);
			}
			$j++;
		}	
		return $commentaries;
	}
	
	function getWickets($type,$key,$match_id){
		//$info = $this->getFeedInfoWithMatchId($type,$key,$match_id);
		//if(empty($info)){ return false;}
		
		//$type = $info['type'];
		//$key = $info['key'];
		if(!$type OR !$key OR !$match_id) return false;
		
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//match[@id='$match_id']");
		
		if(!$res){return false;}
		
		$wickets = array();
		$i = 0;
		while($res[0]->wickets->inning[$i]){
			$attr = array(
				'name'			=>	isset($res[0]->wickets->inning[$i]->attributes()->name) ?(string) $res[0]->wickets->inning[$i]->attributes()->name :'',
				'team'			=>	isset($res[0]->wickets->inning[$i]->attributes()->team) ?(string) $res[0]->wickets->inning[$i]->attributes()->team :'',
				'inningnum'		=>	isset($res[0]->wickets->inning[$i]->attributes()->inningnum) ?(string) $res[0]->wickets->inning[$i]->attributes()->inningnum :'',
			);
			$wicket = array();
			$j = 0;
			while($res[0]->wickets->inning[$i]->wicket[$j]){
				$wicket[] = array(
					'post'		=> isset($res[0]->wickets->inning[$i]->wicket[$j]->attributes()->post) ?(string) $res[0]->wickets->inning[$i]->wicket[$j]->attributes()->post :'',
					'point'		=> isset($res[0]->wickets->inning[$i]->wicket[$j]->attributes()->point) ?(string) $res[0]->wickets->inning[$i]->wicket[$j]->attributes()->point :'',
					'name'		=> isset($res[0]->wickets->inning[$i]->wicket[$j]->attributes()->name) ?(string) $res[0]->wickets->inning[$i]->wicket[$j]->attributes()->name :'',
					'score'		=> isset($res[0]->wickets->inning[$i]->wicket[$j]->attributes()->score) ?(string) $res[0]->wickets->inning[$i]->wicket[$j]->attributes()->score :'',
					'id'		=> isset($res[0]->wickets->inning[$i]->wicket[$j]->attributes()->id) ?(string) $res[0]->wickets->inning[$i]->wicket[$j]->attributes()->id :'',
				);
				$j++;
			}
			$wickets[] = array(
				'attributes'	=> $attr,
				'wicket'		=> $wicket	
			);
			$i++;
		}	
		return $wickets;
	}
	
	function getLineups($type,$key,$match_id){
		//$info = $this->getFeedInfoWithMatchId($type,$key,$match_id);
		//if(empty($info)){ return false;}
		
		//$type = $info['type'];
		//$key = $info['key'];
		if(!$type OR !$key OR !$match_id) return false;
		
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//match[@id='$match_id']");
		
		if(!$res){return false;}
		
		$lineups = array();
		$localteam = array();
		$i = 0;
		while($res[0]->lineups->localteam->player[$i]){
			$localteam[] = array(
				'name'			=>	isset($res[0]->lineups->localteam->player[$i]->attributes()->name) ?(string) $res[0]->lineups->localteam->player[$i]->attributes()->name :'',
				'id'			=>	isset($res[0]->lineups->localteam->player[$i]->attributes()->id) ?(string) $res[0]->lineups->localteam->player[$i]->attributes()->id :'',
			);
			$i++;
		}	
		$visitorteam = array();
		$i = 0;
		while($res[0]->lineups->visitorteam->player[$i]){
			$visitorteam[] = array(
				'name'			=>	isset($res[0]->lineups->visitorteam->player[$i]->attributes()->name) ?(string) $res[0]->lineups->visitorteam->player[$i]->attributes()->name :'',
				'id'			=>	isset($res[0]->lineups->visitorteam->player[$i]->attributes()->id) ?(string) $res[0]->lineups->visitorteam->player[$i]->attributes()->id :'',
			);
			$i++;
		}
		$lineups = array(
			'localteam'		=> $localteam,
			'visitorteam'	=> $visitorteam
		);		
		return $lineups;
	}	
	
	function getMatchInfo($type,$key,$match_id){
		//$info = $this->getFeedInfoWithMatchId($type,$key,$match_id);
		//if(empty($info)){ return false;}
		
		//$type = $info['type'];
		//$key = $info['key'];
		if(!$type OR !$key OR !$match_id) return false;
		
		$linkArray = defineFeedArrayData();
		
		if( ! isset($linkArray[$type][$key])) return false;
		
		$feedArray =  $linkArray[$type][$key];
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//match[@id='$match_id']");
		
		if(!$res){return false;}
		
		$matchinfo = array();
		$i = 0;
		while($res[0]->matchinfo->info[$i]){
			$matchinfo[] = array(
				'name'			=>	isset($res[0]->matchinfo->info[$i]->attributes()->name) ?(string) $res[0]->matchinfo->info[$i]->attributes()->name :'',
				'value'			=>	isset($res[0]->matchinfo->info[$i]->attributes()->value) ?(string) $res[0]->matchinfo->info[$i]->attributes()->value :'',
			);
			$i++;
		}	
		
		return $matchinfo;
	}
	
	function getScoresAttrArray($type,$key,$match_id,$team='home'){
	/**	
		$inningArray = $this->getInning($type,$key,$match_id);
		if(!$inningArray){return false;}
		
		$score = $wicket = $over = '';
		$tmp = array();
		if($team == 'home'){
			foreach($inningArray as $k => $v){
				if($v['attributes']['team'] == 'localteam'){
					$tmp[$v['attributes']['inningnum']] =  $v['totals'];
				}
			}
		}else{
			foreach($inningArray as $k => $v){
				if($v['attributes']['team'] == 'visitorteam'){
					$tmp[$v['attributes']['inningnum']] =  $v['totals'];
				}
			}
		}
		
		$maxKey = max(array_keys($tmp));
		
		$scoreArr = array_filter(explode('(',$tmp[$maxKey]['tot']));
		if(!empty($scoreArr)){
			$p1 = explode('/',$scoreArr[0]);
			$score = isset($p1[0]) ? $p1[0]:'';
			$score = trim( str_replace(array('TOT',':'), array('','') , $score) );
			$wicket = isset($p1[1]) ? $p1[1]:'';
			$over = trim( str_replace(array('(',')','overs'),array('','',''),$scoreArr[1]) );
		}
	**/
		$matchinfo  = $this->getCurrentMatchInfo($type,$key,$match_id);
		if(!$matchinfo){return false;}
		
		if($team=='home'){
			$totalscore = $matchinfo['localteam_totalscore'];
			$stat = 	$matchinfo['localteam_stat'];
		}else{
			$totalscore = $matchinfo['visitorteam_totalscore'];
			$stat = 	$matchinfo['visitorteam_stat'];
		}
		
		$score = $wicket = $over = '';
		if(strpos($totalscore,'/') != FALSE){
			list($score,$wicket) = explode('/', $totalscore);
		}else{
			$score = $totalscore;
		}
		 
		$over = trim( str_replace(array('(',')','overs'),array('','',''),$stat) );
		return array(
			'score'		=> $score,
			'wicket'	=> $wicket,
			'over'		=> $over
		);
	}
	
	function getBatsmanNotout($type,$key,$match_id,$team='home'){
		$currentInningArray = $this->getInning($type,$key,$match_id);
		
		if(!$currentInningArray){return false;}
		
		$batsman = $status = $r = $b = $s4 = $s6 = $sr = $bat = $id = '';
		$returnArray = array();
		
		if($team == 'home'){
			$team_feed = 'localteam';
		}else{
			$team_feed = 'visitorteam';
		}
		
		foreach($currentInningArray as $k=>$v){
			if($v['attributes']['team'] == $team_feed){
				$playerNotOut = array();
				foreach($v['batsmanstats'] as $kk=>$vv){
					if($vv['status'] == 'not out'){
						$returnArray[] = array(
							'batsman'	=> $vv['batsman'],
							'status'	=> $vv['status'],
							'r'			=> $vv['r'],
							'b'			=> $vv['b'],
							's4'		=> $vv['s4'],
							's6'		=> $vv['s6'],
							'sr'		=> $vv['sr'],
							'bat'		=> $vv['bat'],
							'id'		=> $vv['id']
						);
					}
				}
			}
		}
		 
		return $returnArray;
	}
	
	function getOverInfo($type,$key,$match_id){
		$commentaries = $this->getCommentaries($type,$key,$match_id);
		if(!$commentaries){return false;}
		
		$overArray = array();
		$indexArray = array();
		if($key != 'livescore'){
			foreach($commentaries as $k=>$v){
				$indexTMP = explode('.',$v['point']);
				$valueTMP = explode(',',$v['name']);
				
				if(count($indexTMP) == 2){
					$overArray[] = array(
						'post'	=> $v['post'],
						'point' => $v['point'],
						'name'  => $v['name'],
						'index' => $indexTMP[0],
						'index_num'	=>$indexTMP[1],
						'name_value' => trim($valueTMP[0]),
						'value'	=> trim($valueTMP[1])
					);
					$indexArray[] = intval($indexTMP[0]);
				}
			}	
		}else{
			foreach($commentaries as $k=>$v){
				$indexTMP = explode('.',$v['over']);
				$valueTMP = explode(',',$v['post']);
				
				$tmpString = isset($valueTMP[1]) ? strtolower(trim($valueTMP[1])) : '';
				$tmpString = str_replace(
								array(
									'(',')','(no ball)','no ball',
									'no','one','two','three','four','five','six','out',
									'wides','wide','runs','run','leg byes','leg bye','legbye','byes','bye'
									),
								array(
										'','','nb','nb',
										0,1,2,3,4,5,6,'W',
										'wd','wd','','','lb','lb','lb','B','B'
									),
								$tmpString
							);
							
				$tmpString = str_replace(' ','',$tmpString);	
				if(strpos($tmpString,'nb') !== false){
					$n = str_replace('nb','',$tmpString);
					$tmpString = $n.'nb';
				}
				
				if(count($indexTMP) == 2){
					$overArray[] = array(
						'post'	=> $v['post'],
						'over' => $v['over'],
						'index' => isset($indexTMP[0]) ? $indexTMP[0]:'',
						'index_num'	=> isset($indexTMP[1]) ? $indexTMP[1]:'',
						//'name_value' => isset($valueTMP[0])? trim($valueTMP[0]) :'',
						'value'	=> $tmpString
					);
					$indexArray[] = isset($indexTMP[0]) ? intval($indexTMP[0]):'';
				}
			}
		}	
		$indexArray = (array_unique($indexArray));
		
		$over = array();
		foreach($indexArray as $k=>$v){
			$over[$v]	= '';
			$value_array = $index_num_array = array();
			foreach($overArray as $kk=>$vv){
				if($vv['index'] == $v){
					$value_array[] = $vv['value'];
					$index_num_array[] = $vv['index_num'];
				}	
			}
		/**	
			$sameKeyValue = array();
			$tmp = array();
			for($i=0; $i<sizeof($index_num_array); $i++){
				for($j=0; $j<sizeof($index_num_array); $j++){
					if($index_num_array[$i] == $index_num_array[$j] AND $j != $i){
						if(!in_array($i, $tmp) AND !in_array($j, $tmp)){
							$sameKeyValue[] = array($i,$j);
							$tmp[] = $i;
							$tmp[] = $j;
						}
					}
				}
			}

			foreach($sameKeyValue as $k=>$vvv){
				unset($index_num_array[$vvv[0]]);
				$index_num_array = array_values($index_num_array);
				unset($value_array[$vvv[0]]);
				$value_array[$vvv[1]] = '1wd';
				$value_array = array_values($value_array);
			}
		**/	
			$value_array = array_reverse($value_array);
			$over[$v] = implode(' ',$value_array);
		/**	$count_all_values_array = array_count_values($index_num_array);
			$wd_check = false;
			foreach($count_all_values_array as $kkk=>$vvv){
				if($vvv > 1){
					$wd_check = true;
					break;
				}
			}
			if($wd_check OR count($index_num_array) > 6 ){
				$over[$v]	= '1wd or wd1';
			}else{
				$value_array = array_reverse($value_array);
				$over[$v] = implode(' ',$value_array);
			}
			**/
		}
		
		$over2 = array();
		foreach($over as $k=>$v){
			$over2[$k] = $v;
		}
		//var_dump($overArray);
		return $over2;
	}
	
//end class
}